<?
    class QueryTools {
        static function Enclose($array, $what_to_add = "'") {
            $keys = array_keys($array);
            foreach ($keys as $str) {
                $array[$str] = $what_to_add . $array[$str] . $what_to_add;
            }
            return $array;
        }
        static function Escape($array) {
            if (!is_array($array))
                $use = array($array);
            else
                $use = $array;
                
            foreach ($use as $key => $item) {
                $use[$key] = addslashes($use[$key]);
                //$use[$key] = preg_replace("/[\/]+/","/",$use[$key]);
            }
            
            if (!is_array($array))
                return $use[0];
            else
                return $use;
        }
        static function CreateQuery($array, $table, $command="insert", $id = null, $idcol = "id") {
            $arrkeys = array_keys($array);
            $array = self::Escape($array);
            
            if ($command == "insert") {
                $query = "INSERT INTO $table (";
                
                $colsarr = self::Enclose(array_keys($array),"`");
                $query .= implode(",",$colsarr);
                $query .= ") VALUES (";
                $valuesarr = self::Enclose($array,"'");
                $query .= implode(",",$valuesarr);
                $query .= ");";
                
                return $query;
            }
            else if ($command == "update") {
                $query = "UPDATE $table SET ";
                $keys = array_keys($array);
                
                $values = array();
                foreach ($keys as $item) {
                    $values[] = "`$item`='$array[$item]'";
                }
                $query .= implode(",",$values);
                
                if ($id != null && $id != '') {
                    $query .= " WHERE `$idcol`='$id'";
                }
                
                $query .= ";";
                return $query;
            }
            else {
                return "Unsupported Command: $command";
            }
        }
        
        static function EscapeQuotes($arr) {
            if (!is_array($arr))
                $arr = array($arr);
                
            foreach ($arr as $key=>$item) {
                $arr[$key] = addslashes($item);
            }
        }
    }
?>